<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/5
 * Time: 23:41
 */
namespace app\common\logic;

use think\Db;
use think\Exception;

class ConsultM
{
    private $user_id = 0;
    private $org_cate_id = 0;
    private $id = 0;
    private $page = 1;
    private $pagesize = 15;

    /*
     * 获取咨询室列表方法
     * @return array
     */
    public function getList()
    {
        $data = Db::table('edu_consult_room')->field('*')
            ->where('org_cate_id', '=', $this->org_cate_id)
            ->where('status', '=', 0)
            ->order('id', 'asc')
            ->paginate(5, false, ['page'=>1])->toArray();
        if(isset($data['data']) && !empty($data['data'])){
            foreach ($data['data'] as $key=>&$val){
                $image = Db::name('adszone_ads')->where('org_cate_id', $this->org_cate_id)->where('mark', 'chat'.($key+1).'_img')->value('imageurl');
                if(empty($image)){
                    $image = Db::name('adszone_ads')->where('org_cate_id', 0)->where('mark', 'chat'.($key+1).'_img')->value('imageurl');
                }
                $val['image'] = $val['image']?:$image;
                $val['createtime'] = date('Y-m-d', $val['createtime']);
            }
        }
        return $data;
    }
    /*
     * 获取咨询室详情
     */
    public function getDetail()
    {
        if($this->id <= 0){
            return array();
        }
        $data = Db::table('edu_consult_room')->field('*')->where('id', '=', $this->id)->find();
        if($data){
            $data['createtime'] = date('Y-m-d', $data['createtime']);
        }
        return $data;
    }

    /*
     * 保存申请
     */
    public function saveData($param)
    {
        try{
            Db::startTrans();
            $param['user_id'] = $this->user_id;
            $param['org_cate_id'] = $this->org_cate_id;
            $param['createtime'] = time();
            $res = Db::table('edu_consult_reply')->insertGetId($param);
            Db::commit();
            return $res;
        }catch (Exception $e){
            Db::rollback();
            return false;
        }
    }

    /**
     * @param $user_id
     * @return $this
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
        return $this;
    }

    /**
     * @return int
     */
    public function getUserId(){
        return $this->user_id;
    }
    /**
     * @param $org_cate_id
     * @return $this
     */
    public function setOrgCateId($org_cate_id)
    {
        $this->org_cate_id = $org_cate_id;
        return $this;
    }

    /**
     * @return int
     */
    public function getOrgCateId()
    {
        return $this->org_cate_id;
    }

    /**
     * @param $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId(){
        return $this->id;
    }

    /**
     * @param $page
     * @return $this
     */
    public function setPage($page)
    {
        $this->page = abs($page) > 0?abs($page):1;
        return $this;
    }

    /**
     * @return int
     */
    public function getPage(){
        return $this->page;
    }

    /**
     * @param $pagesize
     * @return $this
     */
    public function setPagesize($pagesize)
    {
        $this->pagesize = abs($pagesize) > 0?abs($pagesize):15;
        return $this;
    }

    /**
     * @return int
     */
    public function getPagesize(){
        return $this->pagesize;
    }
}